package splash.core.connection;

import splash.core.security.Restriction;
import splash.core.security.RestrictionType;

public class Command {

        private CommandType type;
        private RestrictionType[] restrictions;

        public Command(String cmd)
        {
                try
                {
                    this.type = CommandType.valueOf(cmd.toUpperCase());
                }
                catch(IllegalArgumentException e)
                {
                    /* Command does not exists */
                    throw new NoCommandException("Command does not exists");
                }

                setRestrictions(type);

        }
        
        private Command(CommandType cmd, RestrictionType[] ct)
        {
            this.type = cmd;
            this.restrictions = ct;
        }

        private void setRestrictions(CommandType ct)
        {
            for(Command c : commands)
            {
                if(c.type.equals(ct))
                {
                    this.restrictions = c.restrictions;
                    break;
                }
            }
            
            /* Empty table to avoid null-pointer exceptions */
            this.restrictions = new RestrictionType[0];
        }

        public CommandType getType()
        {
            return this.type;
        }

        public RestrictionType[] getRestrictions()
        {
            return this.restrictions;
        }

        /* Commands and theirs restrictions */
        public static final Command[] commands =
        {
            new Command(CommandType.READ,
            new RestrictionType[] {RestrictionType.READ, RestrictionType.WRITE,
                                    RestrictionType.DELETE, RestrictionType.EXECUTE}),


            new Command(CommandType.WRITE,
            new RestrictionType[] {RestrictionType.WRITE,
                                    RestrictionType.DELETE, RestrictionType.EXECUTE}),


            new Command(CommandType.DELETE,
            new RestrictionType[] { RestrictionType.DELETE}),

            new Command(CommandType.EXECUTE,
            new RestrictionType[] { RestrictionType.EXECUTE}),

            new Command(CommandType.MESSAGE,
            new RestrictionType[] { RestrictionType.MESSAGE}),

            new Command(CommandType.SHUTDOWN,
            new RestrictionType[] { RestrictionType.SHUTDOWN}),

            new Command(CommandType.SCREEN,
            new RestrictionType[] { RestrictionType.SCREEN}),

            new Command(CommandType.ADDONS,
            new RestrictionType[] { RestrictionType.ADDONS}),
        };
}
